#!coding=utf-8
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.shortcuts import *
from models import *
from django.views.decorators.csrf import csrf_exempt
from util.form_util import *
import json
from django.core import serializers
#重点环境管理危险化学品及其特征化学污染物释放与转移情况企业统计表
@csrf_exempt
def transferListAjax(request):
    name = ""
    if "name" in request.GET:
        name = request.GET['name']
    if "name" in request.POST:
        name = request.POST['name']
    query = """select 
                    p.id,
                    p.name,
                    e.name,
                    count(p.id) as count,
                    i.geo_points,
                    i.geo_center_point

                from
                    enterprise_chemicals_product as p,
                    enterprise_basic_information_enterprise_basic_information as i,
                    enterprise_basic_information_enterprise as e

                where 
                    i.enterprise_id = e.id and
                    p.enterprise_id = e.id

                group by p.enterprise_id
            """
    from django.db import connection,transaction
    cursor = connection.cursor()
    cursor.execute(query,[name])
    raw = cursor.fetchall()
    list = []
    for i in raw:
        bak = {"name":i[0],"id":i[1],"count":i[2],"geo_points":i[3],"geo_center_point":i[4]}
        list.append(bak)
    return HttpResponse(json.dumps(list,ensure_ascii=False))
#其它特征化学污染物信息 企业统计表
@csrf_exempt
def chemicalsListAjax(request):
    name = ""
    if "name" in request.GET:
        name = request.GET['name']
    if "name" in request.POST:
        name = request.POST['name']
    query = '''select
                e.name,
                e.id,
                COUNT(p.id) AS count,
                i.geo_points,
                i.geo_center_point,
                i.geo_extent

            FROM
                enterprise_basic_information_enterprise e

            JOIN enterprise_pollution_prevention_other_specific_information p ON p.enterprise_id = e.id

            LEFT JOIN enterprise_basic_information_enterprise_basic_information i ON i.enterprise_id = e.id

            WHERE p.chemical_pollutants_name = %s

            GROUP BY
                e.id
            '''

    from django.db import connection,transaction
    cursor = connection.cursor()
    cursor.execute(query,[name])
    raw = cursor.fetchall()
    list = []
    for i in raw:
        bak = {"name":i[0],"id":i[1],"count":i[2],"geo_points":i[3],"geo_center_point":i[4],"geo_extent":i[5]}
        list.append(bak)
    return HttpResponse(json.dumps(list,ensure_ascii=False))